package com.caterina.activities;

import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import modulesAPI.v2.CollectionButtonListener;
import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MService;
import budget.Service;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;

import controllers.ManagerTask.TaskType;

public class ServicesActivity extends CActivity implements ModuleChangedListener, CollectionButtonListener{
	public ServicesActivity(Container parent) {
		super("SERVICE_ACTIVITY",parent);
	}
	@Override
	public void onStart() {
		this.putTask(TaskType.SERVICE_GET_ALL, "");
		this.createButtons();
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case SERVICE_GET_ALL:{
				this.lastData=(Collection<Service>) data;
				this.createViewer();
				break;
			}
			case SERVICE_ADD:{
				this.putTask(TaskType.SERVICE_GET_ALL, "");
				break;
			}
			case SERVICE_MODIFY:{
				this.putTask(TaskType.SERVICE_GET_ALL, "");
				break;
			}
			case SERVICE_REMOVE:{
				this.putTask(TaskType.SERVICE_GET_ALL, "");
				break;
			}
		default:break;
		}
	}
	private void createAddModule(){
			this.addModule=new MService(null, "NEW_SERVICE", "NEW_SERVICE_DESC", ViewType.WRITABLE);
		this.addModule.addWritableButton(save,cancel);
	}
	private void createButtons(){
		this.saveModifications=new CButton("SAVE_MODIFICATIONS") {
			@Override
			public void onFocus() {}
			
			@Override
			public void onAction() {
				putTask(TaskType.SERVICE_MODIFY, collectionView.getSelectedContent().getContent());
			}
			private static final long serialVersionUID = 1L;
		};
		this.save=new CButton("SERVICE_SAVE") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				putTask(TaskType.SERVICE_ADD, addModule.getContent());
			}
			private static final long serialVersionUID = 1L;
		};
		this.cancel=new CButton("SERVICE_CANCEL") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				createViewer();
			}
			private static final long serialVersionUID = 1L;
		};
	}
	private void createViewer(){
		if (this.collectionView!=null){
			this.collectionView.removeModuleChangedListener(this);
			this.collectionView.removeCollectionButtonListener(this);
		}
		this.createAddModule();
		ArrayList<Module<?>> srvModules=new ArrayList<Module<?>>();
		for (Service srv: lastData){
			MService srvMod= new MService(null, srv,"SERVICE", "SERVICE", ViewType.WRITABLE);
			srvMod.addWritableButton(this.saveModifications);
			srvModules.add(srvMod);
		}
		this.collectionView=new ModCollectionViewer(null,srvModules ,addModule, "SERVICES_LIST", "SERVICES_LIST_DESC", ViewType.WRITABLE);
        this.collectionView.addModuleChangedListener(this);
		this.collectionView.addCollectionButtonListener(this);
		this.add(collectionView);
	}
	@Override
	public void onError(TaskType task, Exception e) {		
	}	
	@Override
	public void onModuleChange(Module<?> mod) {
		this.add(collectionView);
	}
	@Override
	public void onButtonPressed(Module<?> selectedItem, int buttonIndex) {
		if (buttonIndex==ModCollectionViewer.REMOVE)
			this.putTask(TaskType.SERVICE_REMOVE, selectedItem.getContent());
	}
	private CButton saveModifications;
	private CButton save;
	private CButton cancel;
	private Collection<Service> lastData;
	private ModCollectionViewer collectionView;
	private MService addModule;
	private static final long serialVersionUID = 1L;
}
